/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.op.lib;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.util.Log;

public class JSActivity extends FragmentActivity {

    protected GLSurfaceView mView;

    private boolean hasGLES20() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo info = am.getDeviceConfigurationInfo();
        return info.reqGlEsVersion >= 0x20000;
    }

    // ======================
    // interact with native
    // ======================
    public Object receiveMessage(JSONObject op) throws JSONException {
        return null;
    }

    private String getSignature() {
        try {
            PackageInfo info = getPackageManager().getPackageInfo("com.op.mr3", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                return Base64.encodeToString(md.digest(), Base64.DEFAULT);
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        this.initialize();

        OpJsHelper.init(this);// must be called after onCreate
        boolean loaded = jsCreate(getSignature());
        Log.d("mr3", "Activity.onCreate():" + loaded);

        mView = new JSSurfaceView(this);
        if (!loaded) {
            mView.setRenderMode(JSSurfaceView.RENDERMODE_WHEN_DIRTY);
        }
    }

    protected void initialize() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jsDestory();
        Log.d("mr3", "Activity.onDestroy()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        mView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mView.onResume();
    }

    public static native boolean jsCreate(String signature);

    public static native void jsDestory();

    public static native void evalScript(String script);

    public static native String processRequest(String request);
}
